var mahjongSprites = [];

cc.Class({
    extends: cc.Component,

    properties: {
        leftAtlas:{
            default:null,
            type:cc.SpriteAtlas
        },
        iconAtlas:{
            default:null,
            type:cc.SpriteAtlas
        },
        rightAtlas:{
            default:null,
            type:cc.SpriteAtlas
        },
        shaiziAtlas:{
            default:null,
            type:cc.SpriteAtlas
        },
        bottomAtlas:{
            default:null,
            type:cc.SpriteAtlas
        },
        
        bottomFoldAtlas:{
            default:null,
            type:cc.SpriteAtlas
        },
        
        pengPrefabSelf:{
            default:null,
            type:cc.Prefab
        },
        chiPrefabSelf:{
            default:null,
            type:cc.Prefab
        },
        
        pengPrefabLeft:{
            default:null,
            type:cc.Prefab
        },
        
        chiPrefabLeft:{
            default:null,
            type:cc.Prefab
        },
        
        emptyAtlas:{
            default:null,
            type:cc.SpriteAtlas
        },
        
        holdsEmpty:{
            default:[],
            type:[cc.SpriteFrame]
        },
        
        _sides:null,
        _pres:null,
        _foldPres:null,
    },
    
    onLoad:function(){
        if(cc.vv == null){
            return;
        }
        this._sides = ["myself","right","up","left"];
        this._pres = ["M_","R_","B_","L_"];
        this._foldPres = ["B_","R_","B_","L_"];
        cc.vv.mahjongmgr = this; 
        //筒
        for(var i = 1; i < 10; ++i){
            mahjongSprites.push("dot_" + i);        
        }
        
        //条
        for(var i = 1; i < 10; ++i){
            mahjongSprites.push("bamboo_" + i);
        }
        
        //万
        for(var i = 1; i < 10; ++i){
            mahjongSprites.push("character_" + i);
        }
        
        //中、发、白
        mahjongSprites.push("red");
        mahjongSprites.push("green");
        mahjongSprites.push("white");
        
        //东西南北风
        mahjongSprites.push("wind_east");
        mahjongSprites.push("wind_west");
        mahjongSprites.push("wind_south");
        mahjongSprites.push("wind_north");
    },
    
    getMahjongSpriteByID:function(id){
        return mahjongSprites[id];
    },
    
    getMahjongType:function(id)
    {
 
        if(id >= 0 && id < 9){
            //筒
            return 0;
        }
        else if(id >= 9 && id < 18){
            //条
            return 1;
        }
        else if(id >= 18 && id < 27){
            //万
            return 2;
        }
        else if (id >=27 && id < 30){
            //中 发 白
            return 3;
        }
        else if (id >= 30 && id <34)
        {
            //风
            return 4;
        }


    },
    
    getSpriteFrameByMJID:function(pre,mjid){
        var spriteFrameName = this.getMahjongSpriteByID(mjid);
        spriteFrameName = pre + spriteFrameName;
        if(pre == "M_"){
            return this.bottomAtlas.getSpriteFrame(spriteFrameName);            
        }
        else if(pre == "B_"){
            return this.bottomFoldAtlas.getSpriteFrame(spriteFrameName);
        }
        else if(pre == "L_"){
            return this.leftAtlas.getSpriteFrame(spriteFrameName);
        }
        else if(pre == "R_"){
            return this.rightAtlas.getSpriteFrame(spriteFrameName);
        }
    },
    
    getAudioURLByMJID:function(id){

        
        return this.getWZSounds(id);
        // var realId = 0;
        // if(id >= 0 && id < 9){//21~29为筒
        //     realId = id + 21;
        // }
        // else if(id >= 9 && id < 18){//
        //     realId = id - 8;
        // }
        // else if(id >= 18 && id < 27){
        //     realId = id - 7;
        // }else if(id>=27&&id<34)
        // {
        //     realId=id+10;
        // }
        // console.log("getAudioURLByMJID  nv/" + realId + ".mp3");
        // // W_BaiBan
        // // return "nv/" + realId + ".mp3";
        // return "nv/" +"Mah/" +"W_BaiBan" + ".mp3";

    },

    getWZSounds:function(id){
        var realId = -1;
        var pros=["Tong","Tiao","Wan",""];
        var idx=0;
        var fengPai=["HongZhong","FaCai","BaiBan","DongFeng","XiFeng","NanFeng","BeiFeng"];
        if(id >= 0 && id < 9)
        {
            idx=0;
            realId=id+1;
        }else if(id >= 9 && id < 18)
        {
            idx=1;
            realId=id-8;
        }else if(id >= 18 && id < 27)
        {
            idx=2;
            realId=id-17;
        }else if(id>=27&&id<34)
        {
            idx=3;
            var fengIdx=id-27;
            realId=fengPai[fengIdx];
        }
        var pro=pros[idx];
        var soundName="wz/Mah/W_" +pro+ realId + ".mp3";
        // var soundName="nv/" +"Mah/" +"W_BaiBan" + ".mp3";


        console.log(soundName);
        return soundName;

    },

    getActionSounds:function(type)
    {
        var name="";
        switch(type)
        {
            case "chi":name="Chow";break;
            case "peng":name="Pung";break;
            case "gang":name="Kong";break;
            case "hu":name="Mahjong";break;
            case "zimo":name="";break;
        }
        var fileName="wz/Action/W_"+name+".mp3";
        console.log(fileName);
        cc.vv.audioMgr.playSFX(fileName);
        // return fileName;
    },
    
    getEmptySpriteFrame:function(side){
        if(side == "up"){
            return this.emptyAtlas.getSpriteFrame("e_mj_b_up");
        }   
        else if(side == "myself"){
            return this.emptyAtlas.getSpriteFrame("e_mj_b_bottom");
        }
        else if(side == "left"){
            return this.emptyAtlas.getSpriteFrame("e_mj_b_left");
        }
        else if(side == "right"){
            return this.emptyAtlas.getSpriteFrame("e_mj_b_right");
        }
    },
    
    getHoldsEmptySpriteFrame:function(side){
        // console.log("getHoldsEmptySpriteFrame："+side);
        if(side == "up"){
            return this.emptyAtlas.getSpriteFrame("e_mj_up");
        }   
        else if(side == "myself"){
            return null;
        }
        else if(side == "left"){
            return this.emptyAtlas.getSpriteFrame("e_mj_left");
        }
        else if(side == "right"){
            // console.log("getHoldsEmptySpriteFrame：right");
            return this.emptyAtlas.getSpriteFrame("e_mj_right");
        }
    },
    getshaiziSpriteFrame:function(number)
    {
        var s = "shaizi" + number;
        return this.shaiziAtlas.getSpriteFrame(s);
    },
    
    sortMJ:function(mahjongs,dingque){
        var self = this;
        var caishen=dingque;
        mahjongs.sort(function(a,b){
            if(caishen >= 0){
				if(a==b)
				{
					return 0;		
				}
				if(a==caishen)
				{
					return -1;
				}
				if(b==caishen)
				{
					return 1;
				}
				
				if(a==29)
				{
					return caishen-b;
				}
				
				if(b==29){
					return a-caishen;
				}
				
				return a-b;
            }
            return a - b;
        });
    },
    seticon:function()
    {
        return this.iconAtlas.getSpriteFrame("dy_play_wenhao");
    },
    getSide:function(localIndex){

        return this._sides[localIndex];
    },
    
    getPre:function(localIndex){
        return this._pres[localIndex];
    },
    
    getFoldPre:function(localIndex){
        return this._foldPres[localIndex];
    }
});
